﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.Components
{
    public class PageData
    {
        private Aliyeye.Car.Net.UI.Controls.Overlay overlay;
        public Aliyeye.Car.Net.UI.PageDataSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.PageDataSvc.SoapHeaderClass();
        PageDataSvc.PageDataServiceSoapClient client = new Aliyeye.Car.Net.UI.PageDataSvc.PageDataServiceSoapClient();

        Form form = new Form();
        private Aliyeye.Car.Net.UI.Components.Pager pager;

        private int _PageSize = 10;
        private int _PageIndex = 1;
        private int _PageCount = 0;
        private int _TotalCount = 0;
        private string _TableName;//表名
        private string _QueryFieldName = "*";//表字段FieldStr
        private string _OrderStr = string.Empty; //排序_SortStr
        private string _QueryCondition = string.Empty;//查询的条件 RowFilter
        private string _PrimaryKey = string.Empty;//主键
        private string _Group = string.Empty;

        public PageData()
        {
        }

        public PageData(Form form)
        {
            this.form = form;
            this.overlay = new Aliyeye.Car.Net.UI.Controls.Overlay();
            this.overlay.BackColor = System.Drawing.Color.Transparent;
            this.overlay.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Bold);
            this.overlay.ForeColor = System.Drawing.Color.White;
            this.overlay.IndicatorMode = false;
            this.overlay.Location = new System.Drawing.Point(328, 0);
            this.overlay.Name = "overlay";
            this.overlay.Opacity = 50;
            this.overlay.OverlayColor = System.Drawing.Color.Gray;
            this.overlay.ParentForm = form;
            this.overlay.Size = new System.Drawing.Size(79, 23);
            this.overlay.TabIndex = 4;
            this.overlay.Text = "overlay1";
            this.overlay.Visible = false;
            form.Controls.Add(this.overlay);
        }

        public PageData(Form form, Aliyeye.Car.Net.UI.Components.Pager pager, Aliyeye.Car.Net.UI.Controls.Overlay overlay)
        {
            this.form = form;
            this.pager = pager;
            this.overlay = overlay;

            //this.overlay = new Aliyeye.Car.Net.UI.Controls.Overlay();
            //this.overlay.BackColor = System.Drawing.Color.Transparent;
            //this.overlay.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Bold);
            //this.overlay.ForeColor = System.Drawing.Color.White;
            //this.overlay.IndicatorMode = false;
            //this.overlay.Location = new System.Drawing.Point(328, 0);
            //this.overlay.Name = "overlay";
            //this.overlay.Opacity = 50;
            //this.overlay.OverlayColor = System.Drawing.Color.Gray;
            //this.overlay.ParentForm = form;
            //this.overlay.Size = new System.Drawing.Size(79, 23);
            //this.overlay.TabIndex = 4;
            //this.overlay.Text = "overlay1";
            //this.overlay.Visible = false;
            //form.Controls.Add(this.overlay);
        }

        /**/
        /// <summary>
        /// 显示页数
        /// </summary>
        public int PageSize
        {
            get
            {
                return _PageSize;

            }
            set
            {
                _PageSize = value;
            }
        }
        /**/
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            get
            {
                return _PageIndex;
            }
            set
            {
                _PageIndex = value;
            }
        }
        /**/
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                return _PageCount;
            }
        }
        /**/
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount
        {
            get
            {
                return _TotalCount;
            }
        }
        /**/
        /// <summary>
        /// 表名，包括视图
        /// </summary>
        public string TableName
        {
            get
            {
                return _TableName;
            }
            set
            {
                _TableName = value;
            }
        }
        /**/
        /// <summary>
        /// 表字段FieldStr
        /// </summary>
        public string QueryFieldName
        {
            get
            {
                return _QueryFieldName;
            }
            set
            {
                _QueryFieldName = value;
            }
        }
        /**/
        /// <summary>
        /// 排序字段
        /// </summary>
        public string OrderStr
        {
            get
            {
                return _OrderStr;
            }
            set
            {
                _OrderStr = value;
            }
        }
        /**/
        /// <summary>
        /// 查询条件
        /// </summary>
        public string QueryCondition
        {
            get
            {
                return _QueryCondition;
            }
            set
            {
                _QueryCondition = value;
            }
        }
        /**/
        /// <summary>
        /// 主键
        /// </summary>
        public string PrimaryKey
        {
            get
            {
                return _PrimaryKey;
            }
            set
            {
                _PrimaryKey = value;
            }
        }
        /**/
        /// <summary>
        /// Group
        /// </summary>
        public string Group
        {
            get
            {
                return _Group;
            }
            set
            {
                _Group = value;
            }
        }

        public DataSet QueryDataTable()
        {
            Cursor.Current = Cursors.WaitCursor;
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;

            PageDataSvc.PageData value = new Aliyeye.Car.Net.UI.PageDataSvc.PageData();
            value.TableName = _TableName;
            value.PrimaryKey = _PrimaryKey;
            value.OrderStr = _OrderStr;
            value.PageIndex = PageIndex.ToString();
            value.PageSize = PageSize.ToString();
            value.QueryFieldName = _QueryFieldName;
            value.QueryCondition = _QueryCondition;
            value.Group = _Group;

            DataSet ds = null;
            ServiceManipulator.SafeAsyncExecution<PageDataSvc.PageDataServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader, value),
                result =>
                {
                    if (result.Error == null)
                    {
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                        ds = (DataSet)result.Result;
                        if (ds != null)
                        {
                            pager.bindingSource.DataSource = ds.Tables[0];
                            if (ds.Tables[0].Rows.Count <= 0)
                                PageCommon.Notify("暂无数据");
                        }
                        else
                        {
                            PageCommon.Notify("暂无数据");
                        }
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this.form
            );

            //ds = client.GetList(sopaHeader, value);
            //DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "SP_Pagination", parameters);
            //DataSet ds = DbHelperSQL.RunProcedure("SP_Pagination", parameters, "dd");
            _TotalCount = GetTotalCount();
            if (_TotalCount == 0)
            {
                _PageIndex = 0;
                _PageCount = 0;
            }
            else
            {
                _PageCount = _TotalCount % _PageSize == 0 ? _TotalCount / _PageSize : _TotalCount / _PageSize + 1;
                if (_PageIndex > _PageCount)
                {
                    _PageIndex = _PageCount;

                    value.PageSize = _PageSize.ToString();
                    //parameters[4].Value = _PageSize;

                    ds = QueryDataTable();
                }
            }
            return ds;
        }

        public int GetTotalCount()
        {
            string strSql = " select count(1) from " + _TableName;
            if (_QueryCondition != string.Empty)
            {
                strSql += " where " + _QueryCondition;
            }
            return client.ExecuteScalar(sopaHeader, strSql);
            //return int.Parse(SqlHelper.ExecuteScalar(CommandType.Text, strSql, null).ToString());
        }
    }
}